package org.soderiakelvy.model.dao.imp;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.soderiakelvy.model.dao.RolDao;

import org.soderiakelvy.model.domain.PersonalVO;
import org.soderiakelvy.model.domain.ProductoVO;
import org.soderiakelvy.model.domain.RolVO;

import org.soderiakelvy.model.util.HibernateUtil;

public class RolDaoImp implements RolDao, Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void insertRolVO(RolVO rol) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(rol);
        session.getTransaction().commit();
        session.close();
	}

	@Override
	public void updateRolVO(RolVO rol) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.update(rol);
        session.getTransaction().commit();
        session.close();
	}

	@Override
	public void deleteRolVO(RolVO rol) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.delete(rol);
        session.getTransaction().commit();
        session.close();
	}

	@Override
	public List<RolVO> getAllRoles() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(RolVO.class);
		List<RolVO> result = criteria.list();
		session.close();
		return result;
	}

	@Override
	public RolVO getRolVOByRolId(Integer rolId) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(RolVO.class);
        criteria.add(Restrictions.eq("rolId", rolId)).uniqueResult();
        RolVO rVO = (RolVO) criteria.uniqueResult();
        session.close();
        return rVO;
	}

	@Override
	public RolVO getRolVOByNombre(String nombre) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(RolVO.class);
        criteria.add(Restrictions.eq("nombre", nombre)).uniqueResult();
        RolVO rVO = (RolVO) criteria.uniqueResult();
        session.close();
        return rVO;
	}
}
